Ext.ns("CRM.SPComp");

(function() {
  
  var searchPeriodStore=CRM.commons.createComboStore('searchPeriodStoreid','activityreports/searchPeriodTypes','');
  var actionStore=CRM.commons.createComboStore('actionStoreid','activityreports/actionTypes','');
  var statusStore=CRM.commons.createComboStore('statusStoreid','activityreports/statusTypes','');
  var priorityStore=CRM.commons.createComboStore('priorityStoreid','activityreports/priorityTypes' ,'');
  
  
  CRM.SPComp.createActivityDynamicSearch = function(gridId, dataStore, columnModel) {
	  
  var searchCriteriaPanel = {
		xtype	: 'fieldset',
		id		: 'searchCriteriaPanel',
		width	: 1065,
		title	: 'Selection Criteria',
		items	: [{
		     	    	xtype		: 'compositefield',
		     	    	width		: 600,
		     	    	fieldLabel 	: 'Period',
		     	    	labelWidth 	: 60,
		     	    	items		: [{
			     	    	     	    	xtype 		: 'combo',
			     	    	     	    	id         	: 'searchPeriodType',
			     	    	     	    	hiddenName  : 'searchPerioTypeHiddenName',
			     	                        store		: searchPeriodStore,
			     	                        displayField: 'criteria',
			     	                        valueField  : 'id',
			     	                        //value		: 'LastMonth',
			     	                        triggerAction:'all',
			     	                        editable 	: false,
			     	                        listeners	: {
			     	                        				 select : function(){
			     	                        					 		showPeriod();
			     	                        				 		  }
			     	                        			  }
			     	    	     	    },
			     	    	     	    {	xtype	: 'displayfield', value : 'From: ', width : 50},
			     	    	     	    {
			     	    	     	    	xtype	: 'datefield',
			     	    	     	    	id		: 'fromDate'	
			     	    	     	    },
			     	    	     	    {	xtype	: 'displayfield', value : 'To: ', width : 20},
			     	    	     	    {
			     	    	     	    	xtype	: 'datefield',
			     	    	     	    	id		: 'toDate'
			     	    	     	    }
			     	    	     	  ]
		     	    },
		     	    {
		     	    	xtype		: 'compositefield',
		     	    	width		: 600,
		     	    	fieldLabel 	: 'Status',
		     	    	labelWidth 	: 60,
			     	    items		: [
			     	    	     	    {
			     	    	     	    	xtype      	: 'combo',
			     	                        id         	: 'statusType',
			     	                        store		: statusStore,
			     	                        displayField: 'name',
			     	                        valueField  : 'id',
			     	                        //value		: 'Pending',
			     	                        triggerAction:'all',
			     	                        editable 	: false
			     	    	     	    },
			     	    	     	    {	xtype	: 'displayfield', value : 'Priority: ', width : 50},
			     	    	     	    {
			     	    	     	    	xtype		: 'combo',
			     	                        id         	: 'priorityType',
			     	                        store		:  priorityStore,
			     	                        displayField: 'name',
			     	                        valueField  : 'id',
			     	                        //value		: 'High',
			     	                        triggerAction:'all',
			     	                        width      	: 225,
			     	                        editable 	: false
			     	    	     	    }
			     	    	     	   ]
		     	    		   
		     	     },
		     	     {
		     	    	 xtype		: 'compositefield',
		     	    	 width		: 600,
		     	    	 fieldLabel	: 'Activity Type',
		     	    	 lebalWidth	: 60,
		     	    	 items		: [
		     	    	      		   {
										xtype		: 'combo',
										fieldLabel	: 'Activity Type',
									    id         	: 'activityType',
									    store		: actionStore,
									    displayField: 'name',
									    hiddenName	: 'activityTypeHiddenName',
									    valueField  : 'id',
									    triggerAction:'all',
									    editable 	: false
										}
		     	    	      		   ]
		     	     },
			     	],
			     	 buttons : [{
			            	    	xtype	: 'button',
			            	    	id		: 'searchButton',
			            	    	text	: 'Run Report',
			            	    	handler	: function(){
						            	    		var statusCmp = Ext.getCmp('statusType');
						            	    		var priorityCmp = Ext.getCmp('priorityType');
						            	    		var activityCmp = Ext.getCmp('activityType');
						            	    		var fromDateCmp = Ext.getCmp('fromDate');
						            	    		var toDateCmp = Ext.getCmp('toDate');
						            	    		
						            	    		var statusValue = statusCmp.getValue();
						            	    		var priorityValue = priorityCmp.getValue();
						            	    		var activityValue = activityCmp.getValue();
						            	    		var fromDateValue = fromDateCmp.getValue();
						            	    		var toDateValue   = toDateCmp.getValue();
						            	    		
						            	    		var searchGrd = Ext.getCmp(gridId);
						            	        	searchGrd.store.removeAll();
						            	        	searchGrd.store.reload({
						            	            			params: {
						            	            						statusType	: statusValue,
						            	            						priorityType: priorityValue,
						            	            						activityType: activityValue,
						            	            						periodFrom	: fromDateValue,
						            	            						periodTo	: toDateValue
						            	            					}
						            	        					  });
			            	    				}
				            	 },
				            	 {
				            	  xtype :'button',
				                  text : 'Print',
				                  handler : function(){
				                	  
				            		var griddata=Ext.getCmp(gridId).getStore();
				            		if(griddata!= null)
						 			CRM.SPComp.showPrintActivityWindow(griddata);
				            		
				            		var period = Ext.getCmp('searchPeriodType');
				            		var periodLabel = Ext.getCmp('period');
				            		periodLabel.setText(period.getRawValue());
				            		
				            		var statusType = Ext.getCmp('statusType');
				            		var statusTypeLabel = Ext.getCmp('statusLabel');
				            		statusTypeLabel.setText(statusType.getRawValue());
				            		
				            		var priorityType = Ext.getCmp('priorityType');
				            		var priorityLabel = Ext.getCmp('priorityLabel');
				            		priorityLabel.setText(priorityType.getRawValue());
				            		
				            		var activityType = Ext.getCmp('activityType');
				            		var activityTypeLabel = Ext.getCmp('actType');
				            		activityTypeLabel.setText(activityType.getRawValue());
				            		
				            		var fromDate = Ext.getCmp('fromDate'); 
				            		var fromDateLabel = Ext.getCmp('frDate');
				            		fromDateLabel.setText(fromDate.getRawValue());
				            		
				            		var toDate = Ext.getCmp('toDate');	
				            		var toDateLabel = Ext.getCmp('tDate');
				            		toDateLabel.setText(toDate.getRawValue());
				            		
				            	 }
	            }]
	};
	
    var grid = CRM.commons.createGrid(gridId, dataStore, columnModel);
    grid.setHeight(300);
  	var resultGridPanel = {
  	  		xtype	: 'panel',
  	  		id		: 'resultGridPanel',
  	  		title	: 'Result',
  	  		width	: 1065,
  	  		frame	: true,
  	  		items	: [grid]
  	  	};
  	
  	

	var rootPanel = new Ext.Panel({
		xtype	: 'panel',	
		id		: 'rootPanel',	
		padding : '5px',	
		frame	: true,
		items	: [searchCriteriaPanel, resultGridPanel]	
	});

	return rootPanel;
  	};

  	
  	
  	})();